package com.gxa.autoparts.admin.controller;

import com.gxa.autoparts.admin.mapper.UserMapper;
import com.gxa.autoparts.admin.service.DoorImgService;
import com.gxa.autoparts.admin.service.UserService;
import com.gxa.autoparts.admin.service.VipService;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.pojo.DoorImg;
import com.gxa.autoparts.core.pojo.User;
import com.gxa.autoparts.core.pojo.Vip;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userManage")
@Api
public class UserManagerController {
    @Resource
    UserMapper userMapper;
    @Autowired
    UserService userService;
    @Autowired
    VipService vipService;
    @Autowired
    DoorImgService doorImgService;
    @PostMapping("/selectById")
    @ApiOperation("通过id查询返回用户个别需要信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true)
    })
    public Map selectById(Long id){
        User user = userService.selectById(id);
        Map map=new HashMap();
        //map.put("id",user.getId());
        map.put("name",user.getName());
        map.put("phone",user.getPhone());
        map.put("nickName",user.getNickName());
        //map.put("regPort",user.getRegPort());
        map.put("regTime",user.getRegTime());
        map.put("state",user.getState());
        map.put("img",user.getImg());
        map.put("birthday",user.getBirthday());
        map.put("sex",user.getSex());
        map.put("isVip",user.getIsVip());
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(map);
        return jsonResult.getDataMap();
    }
    @PostMapping("/selectAll")
    @ApiOperation("进入页面返回所有数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true)
    })
    public Map selectAll(Integer page,Integer size) throws Exception {
        List<User> users = userService.selectAll(page, size);
        List<User> users1 = userMapper.selectAll2();
        int size1 = users1.size();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(users);
        jsonResult.put("count",size1);
        return jsonResult.getDataMap();
    }
    @PostMapping("/updateState")
    @ApiOperation("修改用户状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true)
    })
    public Map updateState(Long id){
        userService.updateState(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }
    @PostMapping("/selectByNameAndPhoneAndState")
    @ApiOperation("通过名字，电话，用户状态查询得到的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户名字",required = false),
            @ApiImplicitParam(name = "phone",value = "用户电话",required = false),
            @ApiImplicitParam(name = "state",value = "用户状态",required = false),
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true)
    })
    public Map selectByNameAndPhoneAndState(String name,String phone,Boolean state,Integer page,Integer size) throws Exception {
        List<User> users = userService.selectByNameAndPhoneAndState(name, phone, state, page, size);
        List<User> users1 = userMapper.selectByNameAndPhoneAndStateAll(name, phone, state);
        int size1 = users1.size();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(users);
        jsonResult.put("count",size1);
        return jsonResult.getDataMap();
    }
    @PostMapping("/addVip")
    @ApiOperation("申请成为vip资料")
    public Map addVip(@RequestBody Map map){
        int state=0;
        String factoryName=null;
        String doorName=null;
        String name=null;
        String phone=null;
        String address=null;
        String location=null;
        String licenseImg=null;
        Long userId=0L;
        if(map.get("state")!=null){
            state = Integer.parseInt(map.get("state").toString());
        }
        if(map.get("factoryName")!=null){
            factoryName = map.get("factoryName").toString();
        }
        if(map.get("doorName")!=null){
            doorName = map.get("doorName").toString();
        }
        if(map.get("name")!=null){
            name = map.get("name").toString();
        }
        if(map.get("phone")!=null){
            phone = map.get("phone").toString();
        }
        if(map.get("address")!=null){
            address = map.get("address").toString();
        }
        if(map.get("location")!=null){
            location = map.get("location").toString();
        }
        if(map.get("licenseImg")!=null){
            licenseImg = map.get("licenseImg").toString();
        }
        if(map.get("userId")!=null){
            userId = Long.valueOf(map.get("userId").toString());
        }
        Vip vip = new Vip();
        vip.setAddress(address);
        vip.setDoorName(doorName);
        vip.setFactoryName(factoryName);
        vip.setLicenseImg(licenseImg);
        vip.setLocation(location);
        vip.setName(name);
        vip.setPhone(phone);
        vip.setState(state);
        vip.setUserId(userId);
        Vip insert = vipService.insert(vip);
        Object files = map.get("files");
        List list=(List)files;
        for (Object o : list) {
           String imgs= map.get("fileName").toString();
            Long vipId = vip.getVipId();
            DoorImg doorImg = new DoorImg();
            doorImg.setVipId(vipId);
            doorImg.setImg(imgs);
            doorImgService.insert(doorImg);
        }
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    /*{
        "state":"认证状态",
        "factoryName":"维修厂名称",
        "doorName":"门头名称",
        "name":"联系人名字",
        "phone":"联系电话",
        "address":"所在地区",
        "location":"详细地址",
        "licenseImg":"营业执照",
        "userId":"用户id"
        "files":[
        {"fileName":"第一张照片"},
        {"fileName":"第二张照片"}...
                ]
    }*/




}
